<template>
	<view class="">
		<uni-transition :duration="300" :mode-class="['fade']" :styles="maskClass" @click="togglePopup" catchtouchmove="return"
			:show="isPopupShow"></uni-transition>
		<uni-transition :duration="260" :mode-class="['slide-bottom']" :styles="popupTransfromClass" catchtouchmove="return"
			:show="isPopupShow">
			<view class="top-wrapper">
				<view class="num">共{{ totalNum }}件</view>
				<view class="empty" @click="clearAll">
					<image src="../../static/delet.png" class="icon"></image>
					清空购物车
				</view>
			</view>
			<view v-if="cartProducts.length == 0" class="checked-product-wrapper empty">您的购物车是空的</view>
			<scroll-view v-else scroll-y="true" class="checked-product-wrapper">
				<view v-for="(productList, i) in list" :key="i">
					<template v-if="i == index">
						<view class="product-item" v-for="product in productList" :key="product.id">
							<image :src="product.image" mode="aspectFill" class="img"
								@click="handleDetails(product.id)"></image>
							<view class="text">
								<view class="name ell">{{ product.name }}</view>
								<view class="specifications ell" v-if="product.normal">{{ product.normal }}</view>
								<view class="bottom-wrapper">
									<view class="price">¥ {{ product.price }}</view>
									<count :product="product" :count.sync="product.num"></count>
								</view>
							</view>
						</view>
					</template>
				</view>
				<view class="list">
					<view class="product-item" v-for="product in cartProducts" :key="product.id">
						<image :src="product.productImage" mode="aspectFill" class="img"></image>
						<view class="text">
							<view class="name ell">{{ product.productName }}</view>
							<view class="specifications ell" v-if="product.normal">{{ product.normal }}</view>
							<view class="bottom-wrapper">
								<view class="price">¥ {{ product.price }}</view>
								<count :product="product" :count.sync="product.num"
									@onCountChanged="onCountChanged($event, product.productId)"></count>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</uni-transition>
		<view class="container">
			<view class="num-wrapper" @click="togglePopup">
				<image src="/static/cart02.png" class="icon"></image>
				<view v-show="cartProducts.length > 0" class="num">{{ totalNum }}</view>
			</view>
			<view class="total-wrapper" @click="togglePopup">
				<view class="title">合计：</view>
				<view class="con">¥ {{ totalAmount }}</view>
			</view>
			<button type="default" :disabled="totalNum <= 0 || flag" class="btn oran" @click="confirmOrder">
				结算
				<template v-if="totalNum">
					({{ totalNum }})
				</template>
			</button>
		</view>
	</view>
</template>

<script>
	import {
		memberCarItems
	} from '@/api/order.js';
	import count from './count.vue';
	export default {
		components: {
			count
		},
		props: {
	
			shopId: {
				type: String,
				value: ''
			},
			type: {
				type: String,
				value: ''
			},
		},
		

		data() {
			return {
				isPopupShow: false,
				flag: false,
				totalAmount:0,
				totalNum:0,
				cartProducts:[],
				
				maskClass: {
					position: 'fixed',
					bottom: 0,
					top: 0,
					left: 0,
					right: 0,
					'z-index': 10,
					'background-color': 'rgba(0, 0, 0, 0.6)'
				},
				popupTransfromClass: {
					position: 'fixed',
					bottom: 0,
					left: 0,
					right: 0,
					'z-index': 11,
					'background-color': '#fff'
				},
			};
		},
		computed: {
			currentCartProducts: {
				get() {
					console.log(this.cartProducts)
					return this.cartProducts;
				},
				set(val) {
					this.$emit('update:cartProducts', val);
				}
			}
		},

		onLoad() {},
		methods: {
			
			updateData(num,amount,productList){
				this.totalNum = num;
				this.totalAmount = amount;
				this.cartProducts = productList
			},
			confirmOrder() {
				const data = {
					shopId: this.shopId,
					type: this.type
				};
				this.flag = true;
				memberCarItems(data)
					.then(respanse => {
						let cartList = respanse.result[0].details.map(e => e.id);
						let cartIds = cartList.join(',');
						this.flag = false;
						//  buyType 1立即购买 2购物车
						uni.navigateTo({
							url: `/pages/order/confirm-order?buyType=2&cartIds=${cartIds}&loginType=1`
						});
					})
					.catch(err => {
						this.flag = false;
					});
			},
			onCountChanged(value, id) {
				console.log(value, id)
				this.$emit('onListItemChanged', {
					value,
					id
				});
			},
			clearAll() {

				this.$emit('clearAll');
			},
			togglePopup() {
				this.isPopupShow = !this.isPopupShow;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.checked-product-wrapper {
		min-height: 400rpx;
		max-height: 800rpx;
		padding-bottom: 90px;
	}

	.checked-product-wrapper.empty {
		line-height: 400rpx;
		text-align: center;
		color: #666666;
	}

	.top-wrapper {
		display: flex;
		justify-content: space-between;
		padding: 0 25rpx;
		line-height: 65rpx;
		font-size: 28rpx;
		color: #666;

		.empty {
			padding: 14rpx 0 15rpx;
			height: 65rpx;
			line-height: 36rpx;
			box-sizing: border-box;

			.icon {
				display: inline-block;
				vertical-align: top;
				margin: 7rpx 5rpx 0 0;
				width: 28rpx;
				height: 28rpx;
			}
		}
	}

	.container {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 20;
		display: flex;
		justify-content: space-between;
		padding: 5rpx 25rpx 0;
		background-color: #fff;
		min-height: 98rpx;
		box-sizing: border-box;

		.num-wrapper {
			position: relative;

			.icon {
				width: 88rpx;
				height: 88rpx;
			}

			.num {
				position: absolute;
				right: -22rpx;
				top: 2rpx;
				min-width: 32rpx;
				padding: 0 10rpx;
				box-sizing: border-box;
				line-height: 32rpx;
				font-size: 26rpx;
				color: #fff;
				background-color: #ec5d49;
				border-radius: 16rpx;
			}
		}

		.total-wrapper {
			flex: 1;
			margin-left: 35rpx;
			padding-top: 18rpx;

			.title {
				line-height: 32rpx;
				font-size: 22rpx;
				color: #999999;
			}

			.con {
				margin-top: 3rpx;
				line-height: 45rpx;
				font-size: 35rpx;
				color: #2e2e2e;
			}
		}

		.btn {
			width: 207rpx;
			height: 76rpx;
			line-height: 76rpx;
			text-align: center;
			font-size: 30rpx;
			color: #fff;
			border-radius: 38rpx;
			background: linear-gradient(90deg, #eb7c3f, #ea5d2e);
			margin-top: 6px;
		}
	}

	.container::after {
		content: '';
		height: 9px;
		background-image: linear-gradient(to top, rgba(200, 200, 200, 0.3), rgba(200, 200, 200, 0));
		position: absolute;
		top: -9px;
		right: 0;
		left: 0;
	}

	.product-item {
		display: flex;
		padding: 10rpx 24rpx 10rpx 36rpx;

		.img {
			width: 100rpx;
			height: 100rpx;
			border-radius: 10rpx;
			background-color: #f5f5f5;
		}

		.text {
			flex: 1;
			margin-left: 22rpx;
			overflow: hidden;

			.name {
				line-height: 34rpx;
				font-size: 24rpx;
				font-weight: bold;
				color: #080808;
			}

			.specifications {
				line-height: 26rpx;
				font-size: 20rpx;
				color: #818181;
			}

			.bottom-wrapper {
				display: flex;
				justify-content: space-between;

				.price {
					line-height: 56rpx;
					font-size: 30rpx;
					color: #ea5e2e;
				}
			}
		}
	}
</style>
